package _11_整理题目._整理基础._序列化;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Serializable {
    @Test   // 序列化
    public void testObjectOutputStream() {
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("object"));
            Person person = new Person("孙悟空", 580);
            objectOutputStream.writeObject(person);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Test   // 反序列化
    public void testObjectInputStream() {
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("object"));
            Person person = (Person) objectInputStream.readObject();
            System.out.println("person = " + person);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void testInputOutput() {
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("object1"));
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("object1"));
            Person person = new Person("zhao", 333);
            objectOutputStream.writeObject(person);
            person.setName("sun");
            objectOutputStream.writeObject(person);
            
            Person person1 = (Person) objectInputStream.readObject();
            Person person2 = (Person) objectInputStream.readObject();
            System.out.println("person1=person2 = " + (person1 = person2));
            System.out.println("person1 = " + person1);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
